﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NegaEffect : MonoBehaviour
{

    public Material material;

    public Material spineMaterial;//要替换的spine材质球

    public GameObject[] matImg;//子节点图片所有的材质球

    public GameObject[] matSpine;//子节点所有spine的材质球

    public GameObject[] particles;//粒子节点 进入反色要隐藏

    public Dictionary<string, Material> tempMaterials = new Dictionary<string, Material>();//用来暂存spine的材质球
    void Start()
    {

        //挂到EffectUtil上
        YanhuaMMO.EffectUtil.negaEffect = this;
        ////修改图片的材质球
        foreach (GameObject obj in matImg)
        {
            if (obj != null)
            {
                obj.GetComponent<Renderer>().material = material;
            }
        }

    }

    // void Update () {
    // 	if (Input.GetButton("Fire1"))
    // 	{
    // 		Vector2 pos = new Vector2(Input.mousePosition.x / (float) Screen.width,
    // 			Input.mousePosition.y / (float) Screen.height);
    // 		ShowDistortion(pos);
    // 	}
    // }

    public void SetProgress(float progressNum)
    {
        foreach (GameObject obj in matImg)
        {
            if (obj != null)
            {
                Material mat = obj.GetComponent<Renderer>().material;
                mat.SetFloat("_OutProgress", progressNum);
            }
        }

        foreach (GameObject objSpine in matSpine)
        {
            if (objSpine != null)
            {
                Material matSpineMaterial = objSpine.GetComponent<Renderer>().material;
                float progress = matSpineMaterial.GetFloat("_OutProgress");
                if (progressNum > 0 && progress == 0)
                {
                    if (matSpineMaterial.name != spineMaterial.name)
                    {
                        if (tempMaterials.ContainsKey(objSpine.name) == false)
                        {
                            tempMaterials.Add(objSpine.name, matSpineMaterial);
                        }
                        objSpine.GetComponent<Renderer>().material = spineMaterial;
                    }
                    //修改材质球
                    //tempMaterials[objSpine.objectName]
                }
                else if (progressNum == 0)
                {
                    objSpine.GetComponent<Renderer>().material = tempMaterials[objSpine.name];
                    //修改回原来的材质球
                }
                objSpine.GetComponent<Renderer>().material.SetFloat("_OutProgress", progressNum);
            }
        }
        foreach (GameObject particlesObj in particles)
        {
            if (particlesObj != null)
            {
                if (progressNum != 0)
                {
                    particlesObj.SetActive(false);
                }
                else
                {
                    particlesObj.SetActive(true);
                }
            }
        }
    }
}
